<template>
<div class="tags-add">
  <el-dialog
    :title="`${this.formList.id ? '编辑' : '新增'} 弹窗`"
    :visible="dialogVisible"
    width="30%"
    @close="closemet"
    @open="openadd"
  >
    <template>
      <el-form  label-width="80px" ref="from" :model="formList" :rules="rules">
        <el-form-item label="所属学科" prop="subjectID">
          <el-select placeholder="请选择" v-model="formList.subjectID">
               <el-option
                v-for="item in hireList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="tagName" style="width: 300px">
          <el-input v-model="formList.tagName"/>
        </el-form-item>
      </el-form>
    </template>
    <span class="dialog-footer">
      <el-button >取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </span>
  </el-dialog>
  </div>
  </template>

<script>
import { add, update, detail } from '@/api/hmmm/tags.js'
import { simple } from '@/api/hmmm/subjects.js'

export default {
  name: 'TagsAdd',
  props: {
    dialogVisible: {
      type: Boolean
    }
  },
  data () {
    return {
      formList: {
        subjectID: '',
        tagName: ''
      },
      rules: {
        tagName: [{ required: true, message: '请输入标签名称', trigger: 'blur' }],
        subjectID: [{ required: true, message: '请输入标签名称', trigger: 'blur' }]
      },
      hireList: []

    }
  },
  methods: {
    closemet () {
      this.$emit('add')
    },
    confirm () {
      this.$refs.from.validate(async (valid) => {
        if (valid) {
          if (!this.formList.id) {
            await add(this.formList)
          } else {
            await update(this.formList)
          }
          this.$message.success(`${this.formList.id ? '编辑' : '新增'} 成功`)
          this.$refs.from.resetFields()
          this.closemet()
          this.appForm()
          this.$emit('update-list')
        }
      })
    },
    appForm () {
      this.formList = {
        subjectID: '',
        tagName: ''
      }
    },
    // 渲染弹窗下拉列表
    async openadd () {
      const res = await simple()
      this.hireList = res.data
    },
    async fetList (data) {
      const res = await detail(data)
      this.formList = res.data
      this.$emit('update-list')
    }

  }
}
</script>

<style scoped></style>
